package com.controller;

import com.entity.Response;
import com.entity.pojo.LfUser;
import com.service.UserService;
import com.utils.JwtUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("getInfo")
    public Response getInfo() {
        // 获取用户名
        Subject subject = SecurityUtils.getSubject();
        String username = JwtUtil.getUuid((String) subject.getPrincipal());
        String role = userService.getRole(username);
        return new Response().setSuccess().appendMsg("username", username).appendMsg("role", role);
    }

    @GetMapping
    public Response listUser() {
        List<LfUser> users = userService.listUser();
        return new Response().setSuccess().appendMsg("list", users);
    }

    @PostMapping
    public Response editUser(@RequestBody LfUser user) {
        System.out.println(user);
        int res = userService.updateUser(user);
        if (res <= 0) {
            return new Response().setFailed();
        }
        return new Response().setSuccess();
    }

    @DeleteMapping
    public Response deleteUser(@RequestParam("id") Integer id) {
        int res = userService.deleteUser(id);
        if (res > 0) {
            return new Response().setSuccess();
        } else {
            return new Response().setFailed().setErrMsg("删除失败");
        }
    }

}
